package collectors

import (
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/mem"
	"yunzhan/alert-server/internal/types"
)

/**
 * @Author: way lee
 * @Date: 2024/9/10 09:03
 * @Desc:
 */

func CollectLinuxData() (*types.ResourceData, error) {
	// 获取 CPU 使用率
	cpuPercentages, err := cpu.Percent(0, false)
	if err != nil {
		return nil, fmt.Errorf("error collecting CPU data: %w", err)
	}

	// 获取内存使用率
	vmStat, err := mem.VirtualMemory()
	if err != nil {
		return nil, fmt.Errorf("error collecting memory data: %w", err)
	}

	// 获取磁盘使用率
	diskUsage, err := disk.Usage("/")
	if err != nil {
		return nil, fmt.Errorf("error collecting disk data: %w", err)
	}

	return &types.ResourceData{
		CPUUsage:  cpuPercentages[0],
		MemUsage:  vmStat.UsedPercent,
		DiskUsage: diskUsage.UsedPercent,
	}, nil
}
